import types from 'utils/types.js'
import { handlePath } from 'utils'

export default {
  // 用户登录
  [types.USER_LOGIN] (state, userInfo) {
    // console.log(payload)
    state.userInfo = userInfo
    state.isLogin = true
  },

  // 添加用户歌单
  [types.ADD_USER_PLAYLISTS] (state, playlists) {
    state.userPlaylists = playlists
  },

  // 播放或停止
  [types.PLAY_OR_STOP] (state, currentTime) {
    state.playInfo.isPlay = !state.playInfo.isPlay
    // console.log(currentTime)
    state.playInfo.currentTime = currentTime
  },

  // 用户歌单 收藏 mv dj数量
  [types.ADD_USER_SUBCOUNT] (state, userSubcount) {
    state.userSubcount = userSubcount
    // console.log(userSubcount)
  },

  // 播放歌曲
  [types.PLAY_SONG] (state, songData) {
    // console.log(songData)
    // 把原本的currentTime重新设置为0
    state.playInfo.currentTime = 0
    state.playInfo.isPlay = true
    state.playInfo.songData = songData
    state.playInfo.isShowAsideBottom = true
  },

  // 增加history路径
  [types.ADD_HISTORY] (state, path) {
    const history = state.history
    //  如果没有存放history则执行下面的
    if (!history.pathArr) {
      return history.pathArr.push(path)
    }
    // 有history执行这里
    if (path === handlePath(history, 1)) {
      history.prevPath = handlePath(history)
      history.currentIndex++
      if (history.pathArr.length > history.currentIndex) {
        history.nextPath = handlePath(history, 1)
      } else {
        history.nextPath = null
      }
    } else if (path === handlePath(history, -1)) {
      history.nextPath = handlePath(history)
      history.currentIndex--
      if (history.currentIndex > 0) {
        history.prevPath = handlePath(history, -1)
      } else {
        history.prevPath = null
      }
    } else {
      history.prevPath = handlePath(history)
      history.nextPath = null
      history.pathArr.splice(history.currentIndex + 1, history.pathArr.length - history.currentIndex)
      history.pathArr.push(path)
      history.currentIndex = history.pathArr.length - 1
    }
  },

  // 计时器每秒增加1
  [types.ADD_TIMER] (state) {
    state.playInfo.currentTime++
  },

  // 节流函数调整进度
  [types.CHANGE_PROGRESS] (state, changeTime) {
    // 若时间相同则return
    if (state.playInfo.currentTime === changeTime) return false
    state.playInfo.currentTime = changeTime
  },

  // 显示/关闭歌曲详细页
  [types.SHOW_SONG_DETAIL] (state) {
    state.isShowSongDetail = true
  },

  [types.CLOSE_SONG_DETAIL] (state) {
    state.isShowSongDetail = false
  },

  // 播完完毕
  [types.PLAY_END] (state) {
    state.playInfo.isPlay = false
    state.playInfo.currentTime = 0
  }
}
